﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;

namespace WebApplication1
{
    public partial class viewPrefectsBatchesById : System.Web.UI.Page
    {
        StudentAL studentAL;
        string queryString;
        Guid prefectBatchId;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                queryString = Request.QueryString.ToString();
                prefectBatchId = Guid.Empty;

                bool convertableToGuid = Guid.TryParse(queryString, out prefectBatchId);
                if (convertableToGuid)
                {
                    CreateDynamicTable(prefectBatchId);

                }
                else
                {
                    //Wrong prefectBatchId
                }
            }
        }


        private void CreateDynamicTable(Guid prefectBatchId)
        {
            PlaceHolder6.Controls.Clear();


            studentAL = new StudentAL();


            List<PrefectBatchDTO> prefectBatchDTO = new List<PrefectBatchDTO>();
            List<PrefectDTO> prefectsDTO = new List<PrefectDTO>();
            List<StudentDTO> studentDTO = new List<StudentDTO>();

            prefectBatchDTO =   studentAL.GetPrefectBatchById(prefectBatchId);
            prefectsDTO = studentAL.GetPrefectsByPrefectBatchId(prefectBatchId).ToList();
            //studentDTO = studentAL.GetStudentById();

            // Create a Table and set its properties 
            Table tbl = new Table();
            tbl.Width = new Unit("100%");
            tbl.BorderStyle = BorderStyle.Solid;
            // Add the table to the placeholder control
            PlaceHolder6.Controls.Add(tbl);


            Table subtbl = new Table();
            subtbl.Width = new Unit("50%");
            subtbl.BorderStyle = BorderStyle.Solid;

            Label lblPrefectBatchName = new Label();
            lblPrefectBatchName.Text = "Prefect Batch Name";

            Label lblPrefectFrom = new Label();
            lblPrefectFrom.Text = "From ";

            Label lblPrefectTo = new Label();
            lblPrefectTo.Text = "To ";

            Label lblSPrefectDesc = new Label();
            lblSPrefectDesc.Text = "Description";

            //Label lblCommitteeResponsiblity = new Label();
            //lblCommitteeResponsiblity.Text = "Responsibility";

            //Label lblCommitteeContribution = new Label();
            //lblCommitteeContribution.Text = "Contribution";


            List<Label> allHeaderLabels = new List<Label>();

            allHeaderLabels.Add(lblPrefectBatchName);
            allHeaderLabels.Add(lblPrefectFrom);
            allHeaderLabels.Add(lblPrefectTo);
            allHeaderLabels.Add(lblSPrefectDesc);
            // allHeaderLabels.Add(lblCommitteeContribution);

            int i = 0;
            foreach (Label lblHeader in allHeaderLabels)
            {


                TableRow trHeader = new TableRow();
                trHeader.Width = new Unit("50%");

                TableCell tcHeader = new TableCell();
                TableCell tcHeaderValue = new TableCell();

                HyperLink hlValue = new HyperLink();
                if (i % 2 == 0)
                {
                    tcHeader.BackColor = System.Drawing.Color.Blue;
                    tcHeader.ForeColor = System.Drawing.Color.White;

                    tcHeaderValue.BackColor = System.Drawing.Color.Blue;
                    tcHeaderValue.ForeColor = System.Drawing.Color.White;

                }
                else
                {
                    tcHeader.BackColor = System.Drawing.Color.White;
                    tcHeader.ForeColor = System.Drawing.Color.Blue;

                    tcHeaderValue.BackColor = System.Drawing.Color.White;
                    tcHeaderValue.ForeColor = System.Drawing.Color.Blue;
                }

                if (lblHeader.Text == "Prefect Batch Name")
                {
                    hlValue.Text = prefectBatchDTO[0].PrefectBatchName;
                    hlValue.NavigateUrl = "";
                }

                else if (lblHeader.Text == "From ")
                {
                    hlValue.Text =  prefectBatchDTO[0].PrefectFrom.Value.ToShortDateString();
                    
                }

                else if (lblHeader.Text == "To ")
                {
                    hlValue.Text = prefectBatchDTO[0].PrefectTo.Value.ToShortDateString();
                }
                else if (lblHeader.Text == "Description")
                {
                    hlValue.Text = prefectBatchDTO[0].Description;
                }
                /* else if (lblHeader.Text == "Contribution")
                {
                    lblValue.Text = committeeDTO[0].Contribution;
                }*/
                tcHeader.Controls.Add(lblHeader);
                tcHeaderValue.Controls.Add(hlValue);
                trHeader.Cells.Add(tcHeader);
                trHeader.Cells.Add(tcHeaderValue);





                tbl.Rows.Add(trHeader);
                i++;
            }

            //to make sure the continuty of the color chhange like blue & white in order j s being increased froom i 
            int j = i;
           
            foreach (PrefectDTO prefect in prefectsDTO)
            {
                TableRow trPrefect = new TableRow();
                trPrefect.Width = new Unit("50%");

                TableCell tcPrefect = new TableCell();
                TableCell tcPrefectName = new TableCell();

                Label lblPrefectsRole = new Label();
                Label lblPrefectsHPRole = new Label();
                Label lblPrefectsDHPRole = new Label();

                HyperLink hlPrefectName = new HyperLink();
                if (j % 2 == 0)
                {
                    tcPrefect.BackColor = System.Drawing.Color.Blue;
                    tcPrefect.ForeColor = System.Drawing.Color.White;

                    tcPrefectName.BackColor = System.Drawing.Color.Blue;
                    tcPrefectName.ForeColor = System.Drawing.Color.White;

                }
                else
                {
                    tcPrefect.BackColor = System.Drawing.Color.White;
                    tcPrefect.ForeColor = System.Drawing.Color.Yellow;

                    tcPrefectName.BackColor = System.Drawing.Color.White;
                    tcPrefectName.ForeColor = System.Drawing.Color.Yellow;
                }
                //add the label in the midddle of left hand cell
                if ((j - i) == prefectsDTO.Count / 2 && prefectsDTO[j - i].Role == "Senior Prefect")
                {
                    lblPrefectsRole.Text = prefectsDTO[j - i].Role + "(s)";
                }
                else if (prefectsDTO[j - i].Role == "Deputy Head Prefect")
                {
                    lblPrefectsDHPRole.Text = prefectsDTO[j - i].Role ;
                }
                else if (prefectsDTO[j - i].Role == "Head Prefect")
                {
                    lblPrefectsHPRole.Text = prefectsDTO[j - i].Role ;
                }
                else
                {
                    //lblPrefectsRole.Text = "";
                }
                hlPrefectName.Text = studentAL.GetStudentById(prefectsDTO[j - i].StudentId).SingleOrDefault().FullName;
                hlPrefectName.NavigateUrl = "viewStudentById.aspx?" + prefectsDTO[j - i].StudentId;

                tcPrefect.Controls.Add(lblPrefectsHPRole);
                tcPrefect.Controls.Add(lblPrefectsDHPRole);
                tcPrefect.Controls.Add(lblPrefectsRole);
                tcPrefectName.Controls.Add(hlPrefectName);
                trPrefect.Cells.Add(tcPrefect);
                trPrefect.Cells.Add(tcPrefectName);



                tbl.Rows.Add(trPrefect);
                j++;
            }





            ViewState["dynamictable"] = true;

        }
    }
}